package com.alex.controller;

import com.alex.bean.LoginForm;
import com.alex.pojo.User;
import com.alex.service.impl.UserServiceImpl;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginAction extends Action {

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        //        return super.execute(mapping, form, request, response);
        LoginForm lf = (LoginForm) form;
//        System.out.println("LoginAction");
//        boolean queryUser = new UserDaoImpl().queryUser(form);
        boolean userExist = new UserServiceImpl().userExist(lf.getUsername(), lf.getPassword());
        User user = new UserServiceImpl().getUser(lf.getUsername(), lf.getPassword());

        request.setAttribute("username", lf.getUsername());
        request.getSession().setAttribute("user", user);
        request.setAttribute("queryUser", userExist);
        request.setAttribute("msg", super.getResources(request).getMessage(userExist?"login.success": "login.failure"));

        ActionForward forward = mapping.findForward(userExist ? "success" : "failure");
        System.out.println(forward.getPath());
        return forward;
    }
}
